// pages/student/forum/index.js
import { Serve } from '../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    questions: [],
    comments: [],
    current: {},
    comment: ''
  },

  toCreateQuestion() {
    wx.navigateTo({
      url: '../create-question/index'
    })
  },

  async showDetailPost(e) {
    const { index } = e.currentTarget.dataset
    const { questions } = this.data
    const current = questions[index]
    wx.showLoading({ title: '获取评论', mask: true })
    const comments = await Serve.GetComments(current._id)
    wx.hideLoading()
    this.setData({ visible: true, current, comments })
  },

  hideDetail() {
    this.setData({ visible: false, current: {} })
  },

  handleCommentChange(e) {
    this.setData({ comment: e.detail.value })
  },

  async addComment() {
    const { comment, current } = this.data
    if (!comment) {
      wx.showToast({ title: '内容不可空白', icon: 'error', duration: 800, mask: true })
      return 0
    }

    wx.showLoading({ title: '发布评论', mask: true })
    const result = await Serve.AddComment({ question_id: current._id, content: comment })
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const $this = this
    Serve.GetQuestions().then(questions => {
      $this.setData({ questions })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})